package cn.wolfcode.controller;

import cn.wolfcode.domain.SystemDictionary;
import cn.wolfcode.query.QueryObject;
import cn.wolfcode.result.JsonResult;
import cn.wolfcode.service.ISystemDictionaryService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/systemDictionary")
public class SystemDictionaryController {

    @Autowired
    private ISystemDictionaryService systemDictionaryService;

    @RequestMapping("/list")
    public String input(@ModelAttribute("qo")QueryObject qo, Model model){
        //通过权限  限定有权限的用户才能访问
        Subject subject = SecurityUtils.getSubject();
        if (subject.isPermitted("systemDictionary:list")){
            PageInfo<SystemDictionary> pageInfo = systemDictionaryService.list(qo);
            model.addAttribute("result",pageInfo);
            return "systemDictionary/list";
        }else {
            return "commom/nopermission";
        }
    }

    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(SystemDictionary systemDictionary){
        if (systemDictionary.getId()==null){
            systemDictionaryService.insert(systemDictionary);
        }else{
            systemDictionaryService.updateByPrimaryKey(systemDictionary);
        }
        return "redirect:/systemDictionary/list";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id){
       systemDictionaryService.deleteByPrimaryKey(id);
        return JsonResult.success(null);
    }


}
